package com.bigdatastudio.auth.Config;

import com.bigdatastudio.auth.Filter.AuthAccessDeniedHandler;
import com.bigdatastudio.auth.Filter.JwtAuthError;
import com.bigdatastudio.auth.Filter.TokenAuthenticationFilter;
import com.bigdatastudio.auth.utils.RoutePath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @author xiao_jie
 * @date 2022/10/6 15:12
 * @describe SpringSecurity框架配置类，设置拦截权限等相关信息
 */

@Configuration
@SuppressWarnings({"all"})
@EnableWebSecurity
// 开启资源权限注解
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)

public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private JwtAuthError jwtAuthError;
    @Autowired
    private AuthAccessDeniedHandler authAccessDeniedHandler;
    @Autowired
    private TokenAuthenticationFilter tokenAuthenticationFilter;

    /**
     * 加密方法
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }


    /**
     * 获取SpringSecurity 认证管理器对象
     *
     * @return
     * @throws Exception
     */
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and()
                .csrf().disable()
                .exceptionHandling().accessDeniedHandler(this.authAccessDeniedHandler)
                .and()
                .headers().httpStrictTransportSecurity().disable()
                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                // 只放行login接口，其他的都要权限认证
                .authorizeRequests().antMatchers(RoutePath.getPath()).anonymous()
                // 放行获取文件、图片的接口
                .antMatchers(new String[]{"/js/**", "index.html", "favicon.ico", "/api/file/**", "/webjars/**"})
                .permitAll().anyRequest().authenticated()
                .and()
                .addFilterBefore(this.tokenAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
                .exceptionHandling().authenticationEntryPoint(this.jwtAuthError);
    }

    @Autowired
    private UserDetailsService service;

    //在这里完成获得数据库中的用户信息
    //密码一定要加密
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(service).passwordEncoder(new BCryptPasswordEncoder());
    }

}
